package com.homestead.controller;

import com.homestead.bo.AddOrgAuthBo;
import com.homestead.bo.CreateUserBo;
import com.homestead.controller.ao.AddOrgAuthAo;
import com.homestead.controller.ao.CreateUserAo;
import com.homestead.controller.vo.LoginUserInfoVo;
import com.homestead.resp.CommonResponse;
import com.homestead.service.OrgUserService;
import com.homestead.service.UserService;
import com.homestead.utils.CommonResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author dell
 */
@RestController
@RequestMapping("/user")
@Api(tags = {"用户中心"})
@Slf4j
@RequiredArgsConstructor(onConstructor = @_(@Autowired))
public class UserController {
    private final UserService userService;
    private final OrgUserService orgUserService;
    private final RedissonClient redissonClient;

    @PostMapping("/create")
    @ApiOperation(value = "创建用户", httpMethod = "POST")
    public CommonResponse<Boolean> createUser(@RequestBody CreateUserAo ao) throws Exception {
        CreateUserBo bo = ao.convert();
        boolean result = userService.create(bo);
        return CommonResponseUtil.successObj(result);
    }

    @PostMapping("/addOrgAuth")
    @ApiOperation(value = "给用户添加组织权限", httpMethod = "POST")
    public CommonResponse<Boolean> addOrgAuth(@RequestBody AddOrgAuthAo ao) throws Exception {
        AddOrgAuthBo bo = ao.convert();
        orgUserService.addOrgAuthToUser(bo);

        return CommonResponseUtil.successObj(true);
    }

    @GetMapping("/signIn")
    @ApiOperation(value = "登录", httpMethod = "GET")
    public CommonResponse<LoginUserInfoVo> signIn(String username, String password) {
        return CommonResponseUtil.successObj(userService.signIn(username, password));
    }
}
